package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.MyResponseResult;
import com.itheima.service.DeptService;
import com.itheima.utils.MyThreadLocalUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 部门管理Controller
 */
@RestController
@RequestMapping("depts")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping
    public MyResponseResult<List<Dept>> selectAll(){
        Map<String, Object> value = MyThreadLocalUtils.getValue();
        List<Dept> depts = deptService.selectAllDept();
        return MyResponseResult.ok(depts);
    }

    @DeleteMapping("/{id}")
    public MyResponseResult<Object> delById(@PathVariable Integer id){

        int i = deptService.delById(id);
        /*if(i>=1){
            return MyResponseResult.ok("ok");
        } else  return MyResponseResult.error("id错误");*/
        return i>=1?MyResponseResult.ok("ok"):MyResponseResult.error("id错误");
    }

    @PostMapping
    public MyResponseResult<Object> saveDept(@RequestBody Dept dept){
        int i = deptService.insertDept(dept);
        return i>=1?MyResponseResult.ok(dept.getId()):MyResponseResult.error("添加失败");
    }
}
